<?php

namespace App\Admin\Controllers\Shop;

use App\Admin\Actions\Extensions\ShopClassifyConfigButton;
use App\Admin\Repositories\Shop\WxShopClassify;
use App\Models\Shop\WxShopClassify as ShopClassify;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Tree;

class WxShopClassifysController extends AdminController
{
    private $options = [0=>'不显示', 1=>'总是显示', 2=>'审核时不显示'];

    public function index(Content $content)
    {


        return $content->header('商品分类3层结构')->description('这里主要调整类别结构')
            ->body(function (Row $row) {
                $tree = new Tree(new WxShopClassify);
                $row->column(12, $tree);
                $tree->maxDepth(3);
                $tree->branch(function ($branch) {
                    if(_empty_($branch['pic'])){
                        $logo = '<div style="width:25px;height:25px;margin-left:10px;display: inline-block;"></div>';
                    }else{
                        $logo = "<img src='" . $branch['pic'] ."' style='width:25px;height:25px;margin-left:10px;' class='img'/>";
                    }
                    if(_empty_($branch['bg_img'])){
                        $bg = '<div style="width: 50px;margin-left: 10px;display: inline-block;"></div>';
                    }else{
                        $bg = "<img src='" . $branch['bg_img'] ."' style='width:50px;height:20px;margin-left:10px;opacity: 0.5;' class='img'/>";
                    }

                    $depth = $branch['depth'];
                    $tip = '';
                    $margin_left = 160;
                    if($depth == 2){
                        $margin_left = 130;
                    }else if($depth == 3){
                        $margin_left = 100;
                    }

                    if($branch['in_mp'] == 2){
                        $tip .= '<span style="width: 100px;margin-left: '.(_empty_($tip) ? $margin_left : 100).'px;">小程序端：审核时不显示</span>';
                    }else if($branch['in_mp'] === 0){
                        $tip .= '<span style="width: 100px;margin-left: '.(_empty_($tip) ? $margin_left : 100).'px;">小程序端：不显示</span>';
                    }else{
                        $tip .= '<span style="width: 100px;margin-left: '.(_empty_($tip) ? $margin_left : 100).'px;"></span>';
                    }

                    if($branch['in_app'] == 2){
                        $tip .= '<span style="width: 100px;margin-left: '.(_empty_($tip) ? $margin_left : 100).'px;">App端：审核时不显示</span>';
                    }else if($branch['in_app'] === 0){
                        $tip .= '<span style="width: 100px;margin-left: '.(_empty_($tip) ? $margin_left : 100).'px;">App端：不显示</span>';
                    }else{
                        $tip .= '<span style="width: 100px;margin-left: '.(_empty_($tip) ? $margin_left : 100).'px;"></span>';
                    }

                    if($branch['in_h5'] == 2){
                        $tip .= '<span style="width: 100px;margin-left: '.(_empty_($tip) ? $margin_left : 100).'px;">H5端：审核时不显示</span>';
                    }else if($branch['in_h5'] === 0){
                        $tip .= '<span style="width: 100px;margin-left: '.(_empty_($tip) ? $margin_left : 100).'px;">H5端：不显示</span>';
                    }else{
                        $tip .= '<span style="width: 100px;margin-left: '.(_empty_($tip) ? $margin_left : 100).'px;"></span>';
                    }

                    return "{$branch['id']} - {$branch['name']} $logo $bg $tip";
                });
            });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxShopClassify(), function (Grid $grid) {
            $grid->simplePaginate();
            $grid->model()->where('depth', 3)->orderBy('id', 'desc');
            $grid->quickSearch(['id', 'name','intro'])->placeholder('搜索分类ID，名字，介绍...');
            $grid->column('id')->sortable();
            $grid->column('pic')->image('', 60);
            $grid->column('bg_img')->image('', 60);
            $grid->column('intro');
            $grid->column('parent_id')->select(ShopClassify::pluck('name', 'id'));
            $grid->column('sort')->editable();
//            $grid->column('is_highlight', '高亮')->switch();
//            $grid->column('is_hot', '热榜')->switch();
            $grid->column('state')->switch();


            $grid->column('in_h5')->select($this->options);
            $grid->column('in_app')->select($this->options);
            $grid->column('in_mp')->select($this->options);
//            $grid->column('created_at');

//            $grid->fixColumns(1);
            $grid->model()->orderBy('id', 'asc');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->tools([
               new ShopClassifyConfigButton()
            ]);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxShopClassify(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('intro');
            $show->field('pic');
            $show->field('bg_img');
            $show->field('parent_id');
            $show->field('sort');
            $show->field('state');
            $show->field('in_h5');
            $show->field('in_app');
            $show->field('in_mp');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxShopClassify(), function (Form $form) {
            $form->display('id');
            $options = ShopClassify::where('depth', '<>', 3)->pluck('name', 'id')->toArray();
            $options[0] = '顶级分类';
            $form->select('parent_id', '父级分类')->help('第一层级的请选择“顶级分类”')->options($options)->default(0);

            $form->text('name', '名字');
            $form->text('intro', '介绍');
            $form->image('pic', '图标')->help('建议尺寸：750*750')
                ->url('files/uploads')->uniqueName()->autoUpload();
            $form->image('bg_img', '背景')->help('现在用在类目榜单的位置，宽高比：2.7')
                ->url('files/uploads')->uniqueName()->autoUpload();
            $form->number('sort', '排序');
            $form->switch('state', '状态')->default(1);
//            $form->switch('is_highlight', '高亮');
//            $form->switch('is_hot', '热榜');

            $form->radio('in_h5', 'H5端')->options($this->options)->default(1);
            $form->radio('in_app', 'App端')->options($this->options)->default(1);
            $form->radio('in_mp', '小程序端')->options($this->options)->default(1);

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
